package com.example.petshopping.ui.message.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.ui.message.model.Message;

import java.util.List;

public class MessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<Message> messageList;
    private OnMessageClickListener listener;
    
    // 不同的ViewType
    private static final int VIEW_TYPE_SYSTEM = 1;
    private static final int VIEW_TYPE_NEW_PRODUCT = 2;
    private static final int VIEW_TYPE_NORMAL = 3;
    
    public interface OnMessageClickListener {
        void onMessageClick(Message message);
    }
    
    public MessageAdapter(Context context, List<Message> messageList, OnMessageClickListener listener) {
        this.context = context;
        this.messageList = messageList;
        this.listener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        Message message = messageList.get(position);
        return message.getType();
    }
    
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        
        switch (viewType) {
            case Message.TYPE_SYSTEM:
                View systemView = inflater.inflate(R.layout.item_message_system, parent, false);
                return new SystemMessageViewHolder(systemView);
                
            case Message.TYPE_NEW_PRODUCT:
                View newProductView = inflater.inflate(R.layout.item_message_new_product, parent, false);
                return new NewProductMessageViewHolder(newProductView);
                
            case Message.TYPE_NORMAL:
            default:
                View normalView = inflater.inflate(R.layout.item_message_normal, parent, false);
                return new NormalMessageViewHolder(normalView);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Message message = messageList.get(position);
        
        if (holder instanceof SystemMessageViewHolder) {
            bindSystemMessage((SystemMessageViewHolder) holder, message);
        } else if (holder instanceof NewProductMessageViewHolder) {
            bindNewProductMessage((NewProductMessageViewHolder) holder, message);
        } else if (holder instanceof NormalMessageViewHolder) {
            bindNormalMessage((NormalMessageViewHolder) holder, message);
        }
    }
    
    private void bindSystemMessage(SystemMessageViewHolder holder, Message message) {
        holder.tvTitle.setText(message.getTitle());
        holder.tvTag.setText(message.getTag());
        holder.tvTime.setText(message.getTime());
        holder.tvContent.setText(message.getContent());
        holder.imgIcon.setImageResource(message.getIcon());
        
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onMessageClick(message);
            }
        });
    }
    
    private void bindNewProductMessage(NewProductMessageViewHolder holder, Message message) {
        holder.tvTitle.setText(message.getTitle());
        holder.tvTag.setText(message.getTag());
        holder.tvContent.setText(message.getContent());
        
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onMessageClick(message);
            }
        });
    }
    
    private void bindNormalMessage(NormalMessageViewHolder holder, Message message) {
        holder.tvTitle.setText(message.getTitle());
        holder.tvContent.setText(message.getContent());
        holder.imgIcon.setImageResource(message.getIcon());
        holder.imgRight.setImageResource(message.getRightImage());
        
        // 设置未读消息数量徽章
        if (message.getBadgeCount() > 0) {
            holder.tvBadge.setVisibility(View.VISIBLE);
            holder.tvBadge.setText(String.valueOf(message.getBadgeCount()));
        } else {
            holder.tvBadge.setVisibility(View.GONE);
        }
        
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onMessageClick(message);
            }
        });
    }

    @Override
    public int getItemCount() {
        return messageList.size();
    }
    
    // 系统消息ViewHolder
    static class SystemMessageViewHolder extends RecyclerView.ViewHolder {
        ImageView imgIcon;
        TextView tvTitle;
        TextView tvTag;
        TextView tvTime;
        TextView tvContent;
        
        public SystemMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            imgIcon = itemView.findViewById(R.id.img_message_icon);
            tvTitle = itemView.findViewById(R.id.tv_message_title);
            tvTag = itemView.findViewById(R.id.tv_message_tag);
            tvTime = itemView.findViewById(R.id.tv_message_time);
            tvContent = itemView.findViewById(R.id.tv_message_content);
        }
    }
    
    // 新品开拍ViewHolder
    static class NewProductMessageViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        TextView tvTag;
        TextView tvContent;
        
        public NewProductMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_message_title);
            tvTag = itemView.findViewById(R.id.tv_message_tag);
            tvContent = itemView.findViewById(R.id.tv_message_content);
        }
    }
    
    // 普通消息ViewHolder
    static class NormalMessageViewHolder extends RecyclerView.ViewHolder {
        ImageView imgIcon;
        TextView tvTitle;
        TextView tvContent;
        ImageView imgRight;
        TextView tvBadge;
        
        public NormalMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            imgIcon = itemView.findViewById(R.id.img_message_icon);
            tvTitle = itemView.findViewById(R.id.tv_message_title);
            tvContent = itemView.findViewById(R.id.tv_message_content);
            imgRight = itemView.findViewById(R.id.img_message_right);
            tvBadge = itemView.findViewById(R.id.tv_message_badge);
        }
    }
} 